package com.hornsun.webConfig;

import com.hornsun.configInfo.ConfigInfo;
import com.hornsun.configInfo.ConfigListInfo;
import com.hornsun.database.IRedisService;
import com.hornsun.database.SerializeUtils;
import com.hornsun.database.WechatInfo;
import com.hornsun.domain.constant.error.CommonError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


/**
 * Created by 编译中... on 2018/5/22.
 */
@Configuration
public class WebSecurityConfig extends WebMvcConfigurationSupport {

    @Autowired
    private IRedisService service;

    @Autowired
    private ConfigInfo configInfo;

    @Autowired
    private ConfigListInfo configListInfo;

    @Bean
    public WebAdminInterceptor getWebAdminInterceptor() {
        return new WebAdminInterceptor();
    }

    @Bean
    public WechatAdminInterceptor getWechatAdminInterceptor(){
        return new WechatAdminInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //排除的路径

       /* addInterceptor.excludePathPatterns("/admin*//**");*/

        //拦截所有路径
        List<String> webUrl = configListInfo.getWeb();
        List<String> wechatUrl = configListInfo.getWechat();
        for (String url : webUrl){
            registry.addInterceptor(getWebAdminInterceptor()).addPathPatterns(url);
        }
        for (String url : wechatUrl){
            registry.addInterceptor(getWechatAdminInterceptor()).addPathPatterns(url);
        }
    }

    private class WebAdminInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
            HttpSession session = request.getSession();
            if (session.getAttribute(configInfo.getSessionKey())!= null){
                return true;
            }else {
                response.setStatus(CommonError.NOT_LOGIN.getCode());
                return false;
            }
        }
    }

    private class WechatAdminInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
            String sessionId = request.getHeader("sessionId");
            if (sessionId != null){
                // session存在
                if (service.isKeyExists(sessionId)&&!service.isOverdue(sessionId)){
                    byte[] bytes = (byte[]) service.get(sessionId);
                    WechatInfo wechatInfo = (WechatInfo) SerializeUtils.deSerialize(bytes);
                    request.setAttribute("openid",wechatInfo.getOpenid());
                    return true;
                }else{
                    response.setStatus(CommonError.NOT_LOGIN.getCode());
                    return false;
                }

            } else {
                response.setStatus(CommonError.NOT_LOGIN.getCode());
                return false;
            }
        }
    }

}
